# 测试类
# jwt原理测试
import base64 ,json,time,hashlib

# 头部信息
heard_data = {'typ':'jwt','alg':"Hs256"}
# 将字典格式转成字符串
# json.dumps(heard_data)
# 将字符串赚称字节对象
head = base64.b64encode( json.dumps(heard_data).encode() ).decode()
print('........Header头部Base64信息....')
print(head)


# 载荷部分：payload
iat = int(time.time())
payload_data = {
    "sub":"admin",
    "exp":iat+60*60,
    "iat":iat,
    "name":"chaojiyonghu",
    "user_id":1,
    "admin":True,
    "acc_password":"asjdfalkfdjviaupwuradjPAHAFJSDF"
}

payload = base64.b64encode(json.dumps(payload_data).encode()).decode()
print('........payload载荷部分Base64信息....')
print(payload)


# 第三部分：签证
secret = 'django-insecure-mwap67jc^4l5db+xw55=8ou2*b)&nlo%x76i2x0w-(k+#9kevn'
data = head+payload+secret
hs256 = hashlib.sha256()
# 传入数据，并修改编码格式
hs256.update(data.encode('utf-8'))
# 获取最终加密字符串
signtrue = hs256.hexdigest()
print('第三部分:签证')
print(signtrue)

# 最终jwt数据
token = f'{head}.{payload}.{signtrue}'


print('------最终jwt数据格式-----------')
print(token)